<?php
namespace app\admin\controller;
use \think\Controller;
use app\admin\model\Company as CompanyModel;

class Company extends Controller
{

    protected $CompanyModel;

    public function __construct()
    {
        if(!session('adminId')){
            $this->redirect('login/index');
        }
        $this->CompanyModel = new CompanyModel();
        $this->gid = session('adminGid');
    }

    public function index()
    {
        $query = [
            'ispage' => true,
            'limit' => 12,
            'where' => [
                'gid' => $this->gid
            ],
            'order' => [
                'update_time' => 'desc'
            ]
        ];
        $companyRes = $this->CompanyModel->getLst($query);
        return view('index',[
            'company'=> $companyRes
        ]);
    }

    public function add()
    {
        if(request()->isPost()){
            $data = input();
            $data['gid'] = $this->gid;
            $res =  $this->CompanyModel -> save($data);
            if($res){
                $this->success('新增成功','company/index');
            }else{
                $this->error('新增失败,请检查重试'); 
            }
        };
        return view();
    }

    public function edit($id)
    {
        $companyRes = $this->CompanyModel->get($id);
        if(request()->isPost()){
            $data = input();
            $data['gid'] = $this->gid;
            $res =  $companyRes -> save($data);
            if($res){
                $this->success('编辑成功','company/index');
            }else{
                $this->error('编辑失败,请检查重试'); 
            }
        }
        return view('edit',[
            'company' => $companyRes
        ]);
    }

    public function del($id)
    {
        if(request()->isPost()){
            $CompanyModel = new CompanyModel();
            $res =  $CompanyModel -> destroy($id);
            if($res){
                return 1;
            }else{
                return 0;
            }
        };
    }


}
